﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using QLHD;
using System.Data.SqlClient;
public partial class DangNhap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Visible = false;
        if (!IsPostBack)
        {
            if (Request.Cookies["cRemember"] != null)
            {
                HttpCookie httpCookie = Request.Cookies["cUserAndTime"];
                txtUserName.Text = httpCookie.Values["cUserName"].ToString();
                txtPassWord.Text = httpCookie.Values["cPassWord"].ToString();
            }
        }
    }
    void WriteCookie(string strUserName, string strPassWord)
    {
        if (CheckBoxRemember.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("cRemember", "Yes");
            Response.Cookies.Add(httpCookie);//thêm đối tượng cookie cRemember
            httpCookie = new HttpCookie("cUserAndTime");
            httpCookie.Values.Add("cUserName", strUserName);
            httpCookie.Values.Add("cPassWord", strPassWord);
            httpCookie.Values.Add("Time", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);//thêm đối tượng cookie cUserAndTime
        }
    }




    protected void btDangNhap_Click(object sender, EventArgs e)
    {
        //taikhoan tk = new taikhoan();
        //tk.UserName = txtUserName.Text;
        //tk.Password = txtPassWord.Text;
        //try
        //{
        //    string[] result = tk.Login();
        //    if (result[0] == "OK")
        //    {
        //        //kiem  pass word va thong bao
        //        this.WriteCookie(txtUserName.Text, txtPassWord.Text);//lưu Cookie
        //        // luu thong tin vao session
        //        Session.Add("UserName", result[2]);
        //        Session.Add("PassWord", result[3]);
        //        Session.Add("QuyenSuDung", result[5]);
        //        Session.Add("NhanVien", result[6]);
        //        Response.Redirect("QuanTriHeThong.aspx");
        //    }

        //    else//UserName hoặc Password không hợp lệ
        //    {
        //        switch (result[0])
        //        {
        //            case "U":
        //                lblError.Visible = true;
        //                lblError.Text = "Không tìm thấy tài khoản !";
        //                break;
        //            case "P":

        //                lblError.Visible = true;
        //                lblError.Text = "Mật khẩu sai, vui lòng kiểm tra lại !";
        //                break;
        //        }
        //    }
        //}
        //catch (SqlException)
        //{

        //    lblError.Visible = true;
        //    lblError.Text = "Loi ket noi, vui lòng kiểm tra lại !";
        //}
        

    }
    protected void btHuy_Click(object sender, EventArgs e)
    {
        txtUserName.Text = null;
        txtPassWord.Text = null;
        CheckBoxRemember.Checked = false;
    }
}
